package com.lcq.study.pdf.jasper;

import com.lcq.study.pdf.dto.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName JasperReportTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/6/24 10:07
 * @Version 1.0
 **/
public class AllTest {
    public static void main(String[] args) throws JRException {


        String jasperFilePath = "C:/Users/86156/JaspersoftWorkspace/MyReports";


        List<JasperPrint> jasperPrintList = new ArrayList<>();
        JasperFillManager jasperFillManager = JasperFillManager.getInstance(DefaultJasperReportsContext.getInstance());

        addTitlePage(jasperFillManager, jasperFilePath, jasperPrintList);

        addPredictExportPage(jasperFillManager, jasperFilePath, jasperPrintList);

        addPMPage(jasperFillManager, jasperFilePath, jasperPrintList);

        addLightPage(jasperFillManager, jasperFilePath, jasperPrintList);

        addMicroClimatePage(jasperFillManager, jasperFilePath, jasperPrintList);

        addEnergyConsumptionPage(jasperFillManager, jasperFilePath, jasperPrintList);

        addCarbonEmissionPage(jasperFillManager, jasperFilePath, jasperPrintList);


        String destFile = "D:/test/pdf/all.pdf";
        JasperUtils.exportPages(destFile, jasperPrintList);
    }

    private static void addTitlePage(JasperFillManager jasperFillManager,
                                                 String jasperFilePath,
                                                 List<JasperPrint> jasperPrintList) throws JRException {
        String name = "BPAReport_1";
        HashMap<String, Object> params = new HashMap<>();
        params.put("accountName","中国建筑东北设计研究院");
        params.put("prjName","东北院项目");
        params.put("fileName","ZH-WJF-YF-33#-S-SG");
        params.put("forecastIndex","颗粒物、光照、微气候、能耗、碳排放");
        params.put("reportDate","2022 年 6 月 8 日");
        params.put("reportNum", "BEPCT2022060001");
        JasperPrint jasperPrint = jasperFillManager.fill(getJasperFile(jasperFilePath, name),
                params,
                new JREmptyDataSource());
        jasperPrintList.add(jasperPrint);

        JasperPrint jasperPrint1 = jasperFillManager.fill(getJasperFile(jasperFilePath, "BPAReport_2"),
                params,
                new JREmptyDataSource());
        jasperPrintList.add(jasperPrint1);
    }

    private static void addPredictExportPage(JasperFillManager jasperFillManager,
                                     String jasperFilePath,
                                     List<JasperPrint> jasperPrintList) throws JRException {
        String name = "BPAReport_4";
        HashMap<String, Object> params = new HashMap<>();
        params.put("accountName","中国建筑东北设计研究院");
        params.put("prjName","东北院项目");
        params.put("fileName","ZH-WJF-YF-33#-S-SG");
        params.put("forecastIndex","颗粒物、光照、微气候、能耗、碳排放");
        params.put("reportDate","2022 年 6 月 8 日");
        params.put("reportNum", "BEPCT2022060001");
        params.put("fileSize", "89.32M");
        params.put("fileFormat", "rvt");
        params.put("prjLocation", "长春");
        params.put("prjStage", "设计");
        params.put("folderName", "ZH-WJF-YF-33#-S-SG");
        params.put("commitDate", "2022 年 5 月 30 日");
        params.put("predictResult", "提交模型共识别出房间共 137 个；\n 申请预测房间 6 个；\n 执行预测房间 6 个；\n 完成预测房间 6 个。");
        params.put("remark", "夏季自然通风，冬季集中供暖");
        JasperPrint jasperPrint = jasperFillManager.fill(getJasperFile(jasperFilePath, name),
                params,
                new JREmptyDataSource());
        jasperPrintList.add(jasperPrint);

        JasperPrint jasperPrint1 = jasperFillManager.fill(getJasperFile(jasperFilePath, "BPAReport_5"),
                params,
                new JREmptyDataSource());
        jasperPrintList.add(jasperPrint1);
    }




    private static void addPMPage(JasperFillManager jasperFillManager,
                                                 String jasperFilePath,
                                                 List<JasperPrint> jasperPrintList) throws JRException {

        String name1 = "BPAReport_6";
        String jasperFile1 = getJasperFile(jasperFilePath, name1);


        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(getPMDataList()));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);

        HashMap<String, Object> params1 = new HashMap<>();
        JasperPrint jasperPrint1 = jasperFillManager.fill(jasperFile1, params1, new JREmptyDataSource());
        jasperPrintList.add(jasperPrint1);

        HashMap<String, Object> params2 = new HashMap<>();
        params2.put("outdoorConditions", "温度:-21.1°C 湿度:66% 风速:3 级 风向：WSW\n" +
                "PM2.5:132μg/m3 PM10:249μg/m3");

        String name2 = "BPAReport_7";
        String jasperFile2 = getJasperFile(jasperFilePath, name2);
        JasperPrint jasperPrint2 = jasperFillManager.fill(jasperFile2, params2, new JRBeanCollectionDataSource(mlist));
        jasperPrintList.add(jasperPrint2);
    }


    private static void addLightPage(JasperFillManager jasperFillManager,
                                  String jasperFilePath,
                                  List<JasperPrint> jasperPrintList) throws JRException {

        String name1 = "BPAReport_8";
        String jasperFile1 = getJasperFile(jasperFilePath, name1);


        HashMap<String, Object> params1 = new HashMap<>();
        JasperPrint jasperPrint1 = jasperFillManager.fill(jasperFile1, params1, new JREmptyDataSource());
        jasperPrintList.add(jasperPrint1);


        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(getLightDataList()));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);

        String name2 = "BPAReport_9";
        String jasperFile2 = getJasperFile(jasperFilePath, name2);
        HashMap<String, Object> params2 = new HashMap<>();
        JasperPrint jasperPrint2 = jasperFillManager.fill(jasperFile2, params2, new JRBeanCollectionDataSource(mlist));
        jasperPrintList.add(jasperPrint2);
    }

    public static List<LightDataDto> getLightDataList(){
        ArrayList<LightDataDto> list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            LightDataDto lightDataDto = new LightDataDto();
            lightDataDto.setRoomNum(""+i);
            lightDataDto.setRoomVolume("53.46");
            lightDataDto.setCtgd("2.5");
            lightDataDto.setBltgl("0.339");
            lightDataDto.setQtclfsl("0.502");
            lightDataDto.setCgxs("3.46");
            lightDataDto.setZycgzd("259.34");
            list.add(lightDataDto);
        }


        return list;
    }





    public static List<PmYCDataDto> getPMDataList(){
        ArrayList<PmYCDataDto> list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            PmYCDataDto pmYCDataDto = new PmYCDataDto();
            pmYCDataDto.setPm10yc("149.865106");
            pmYCDataDto.setPm205yc("79.473840");
            pmYCDataDto.setAirFilterClass("20.53%");
            pmYCDataDto.setRoomNum(""+i);
            pmYCDataDto.setWcmj("11.125");
            pmYCDataDto.setWcqmxdj("");
            pmYCDataDto.setRoomVolume("53.46");
            pmYCDataDto.setRoomUsage("办公室");
            list.add(pmYCDataDto);
        }


        return list;
    }


    private static void addMicroClimatePage(JasperFillManager jasperFillManager,
                                     String jasperFilePath,
                                     List<JasperPrint> jasperPrintList) throws JRException {

        String name1 = "BPAReport_10";
        String jasperFile1 = getJasperFile(jasperFilePath, name1);




        HashMap<String, Object> params1 = new HashMap<>();
        JasperPrint jasperPrint1 = jasperFillManager.fill(jasperFile1, params1, new JREmptyDataSource());
        jasperPrintList.add(jasperPrint1);


        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(getMicroclimateDataList()));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);

        String name2 = "BPAReport_11";
        String jasperFile2 = getJasperFile(jasperFilePath, name2);
        HashMap<String, Object> params2 = new HashMap<>();
        JasperPrint jasperPrint2 = jasperFillManager.fill(jasperFile2, params2, new JRBeanCollectionDataSource(mlist));
        jasperPrintList.add(jasperPrint2);
    }


    public static List<MicroclimateDataDto> getMicroclimateDataList(){
        ArrayList<MicroclimateDataDto> list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            MicroclimateDataDto dataDto = new MicroclimateDataDto();
            dataDto.setRoomNum(""+i);
            dataDto.setRoomVolume("53.46");
            dataDto.setSfxt("下送上回 ");
            dataDto.setSfwd("14 ");
            dataDto.setSffs("0.35");
            dataDto.setRyfh("49.7");
            dataDto.setPmv("0.45");
            list.add(dataDto);
        }


        return list;
    }

    private static void addEnergyConsumptionPage(JasperFillManager jasperFillManager,
                                                 String jasperFilePath,
                                                 List<JasperPrint> jasperPrintList) throws JRException {
        String name = "BPAReport_12";

        JasperPrint jasperPrint = jasperFillManager.fill(getJasperFile(jasperFilePath, name),
                new HashMap<>(),
                new JRBeanCollectionDataSource(getDataSource(getECDataList())));
        jasperPrintList.add(jasperPrint);
    }

    private static void addCarbonEmissionPage(JasperFillManager jasperFillManager,
                                              String jasperFilePath,
                                              List<JasperPrint> jasperPrintList) throws JRException {
        String name = "BPAReport_13";
        JasperPrint jasperPrint = jasperFillManager.fill(getJasperFile(jasperFilePath, name),
                new HashMap<>(),
                new JRBeanCollectionDataSource(getDataSource(getCEDataList())));
        jasperPrintList.add(jasperPrint);
    }

    private static List<ModelTableSource> getDataSource(Collection<?> beanCollection) {
        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(beanCollection));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);
        return mlist;
    }

    private static String getJasperFile(String jasperFilePath, String name) {
        return jasperFilePath + "/" + name + ".jasper";
    }
    public static List<EnergyConsumptionDataDto> getECDataList(){
        ArrayList<EnergyConsumptionDataDto> list = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            EnergyConsumptionDataDto dataDto = new EnergyConsumptionDataDto();
            dataDto.setMonth(""+i);
            dataDto.setMonthColdFh("9.8");
            dataDto.setMonthColdNh("2.8");
            dataDto.setMonthHeatFh("10077");
            dataDto.setMonthHeatNh("3875.6");
            list.add(dataDto);
        }


        return list;
    }

    public static List<CarbonEmissionDataDto> getCEDataList(){
        ArrayList<CarbonEmissionDataDto> list = new ArrayList<>();

        for (int i = 1; i <= 6; i++) {
            CarbonEmissionDataDto dataDto = new CarbonEmissionDataDto();
            dataDto.setMonth1(""+i);
            dataDto.setCarbonEmission1("4892.733564");
            dataDto.setMonth2(""+(i+6));
            dataDto.setCarbonEmission2("8511.065456");
            list.add(dataDto);
        }
        return list;

    }
}
